#ifndef QPROPERTYLISTVIEW_H
#define QPROPERTYLISTVIEW_H

#include "corelibglobal.h"

#include <QTreeWidget>

#include <QMap>
#include <QItemDelegate>
#include <QVector>
#include <QPair>
#include <QColor>
#include <QMap>

namespace SHAREDLIB
{
    class QAbstractProperty;
}

namespace CORELIB
{
    class QPropertyListView;
    class QPropertyBaseEditor;
    class QPropertyEditorDelegate: public QItemDelegate
    {
        Q_OBJECT
    public :
        QPropertyEditorDelegate(QPropertyListView* parent):m_listView(parent){}
        QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

        QTreeWidgetItem     *editerItem();

    protected slots:
        void slotEditorDestroyed(QObject *object);
        void propertyValueChanged();
        void propertyDestroyed();
    protected:
        QPropertyListView   *m_listView;
        mutable QList<QTreeWidgetItem*> m_editedItem;
        mutable QWidget*            m_editor;
        mutable SHAREDLIB::QAbstractProperty *  m_property;
    };

    class CORELIB_EXPORT QPropertyListView: public QTreeWidget
    {
        Q_OBJECT
    public:
        QPropertyListView(QWidget *parent=0);
        void    setPropertys(const QList<SHAREDLIB::QAbstractProperty*> &propertys);

        void    clearAll();

        bool    pagePosition(const QVariant &param);

        void    page_position(QVariant &info);
        void    set_page_position(const QVariant &info);
    public:
        SHAREDLIB::QAbstractProperty    *itemToProperty(QTreeWidgetItem *item) const;
        QTreeWidgetItem                 *indexToItem(const QModelIndex &index)const;
        bool                            isGroupItem(QTreeWidgetItem *item)const;
        int                             groupIndex(QTreeWidgetItem *item)const;
    protected slots:
        void    propertyShowValueChanged();
        void    propertyAttributeChanged(const QString &key);
        void    edit();
        void    cammand_property_redo();
        void    cammand_property_undo();
        void    property_value_changed(const QVariant & old,const QVariant &now);
    protected:
        void    updateView();
        void    insertItem(SHAREDLIB::QAbstractProperty* property,SHAREDLIB::QAbstractProperty* parent=NULL);

        void    drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const;
        void    mousePressEvent(QMouseEvent *event);

        void    closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);

    signals:
        void    need_make_page_position();
    private:
        friend class QPropertyEditorDelegate;
    protected:
        QList<SHAREDLIB::QAbstractProperty*>                    m_propertys;
        QMap<QString,QTreeWidgetItem*>                          m_groupItems;
        QList<QTreeWidgetItem*>                                 m_groups;
        QMap<SHAREDLIB::QAbstractProperty*,QTreeWidgetItem*>    m_propertyToItem;
        QMap<QTreeWidgetItem*,SHAREDLIB::QAbstractProperty*>    m_itemToProperty;
        QVector<QPair<QColor,QColor> >                          m_colors;
        QPropertyEditorDelegate                                 *m_delegate;
        QIcon                                                   m_expandIcon;
        QVariant                                                m_page_position;
    };

}

#endif // QPROPERTYLISTVIEW_H
